<?php
/* ******************************
 *	Workers aJax actions
 * ******************************
 */
include('../../includes/classes/core.php');
$action 	= $_REQUEST['act'];
$user_id	= $_SESSION['USERID'];
$error		= '';
$data		= '';

switch ($action) {
	case 'get_add_page':
		$page		= GetPage();
		$data		= array('page'	=> $page);

		break;
	case 'get_edit_page':
	    $barcode_id		= $_REQUEST['id'];
		$page		= GetPage(GetBarcode($barcode_id));

        $data		= array('page'	=> $page);

	    break;
    case 'print_barcode':
    	$printpage		= GetPrintPage();
    	$data		= array('printpage'	=> $printpage);

    	break;
	case 'get_list':
	    $count	= $_REQUEST['count'];
	    $hidden	= $_REQUEST['hidden'];
		$rResult = mysql_query("	SELECT `barcodes`.`id`,
									       `client`.`name`,
										   `production`.`name`,
										   `barcodes`.`old_barcode`,
									       `barcodes`.`code`,
									       `barcodes`.`create_date`,
										   `barcodes`.`comment`
									FROM   `barcodes`
									LEFT JOIN `client` ON `client`.`id` = SUBSTR(`barcodes`.`code`, 2, 4)
									LEFT JOIN `production` ON `production`.`id` = SUBSTR(`barcodes`.`code`, 6, 4)
									WHERE  `barcodes`.`actived` = 1 && ISNULL(`barcodes`.`serial_number`)");

		$data = array(
			"aaData"	=> array()
		);

		while ( $aRow = mysql_fetch_array( $rResult ) )
		{
			$row = array();
			for ( $i = 0 ; $i < $count ; $i++ )
			{
				/* General output */
				$row[] = $aRow[$i];
				if($i == ($count - 1)){
					$row[] = '<input type="checkbox" name="check_' . $aRow[$hidden] . '" class="check" value="' . $aRow[$hidden] . '" />';
				}
			}
			$data['aaData'][] = $row;
		}

        break;
    case 'test_old_barcode':
		$ob=$_REQUEST['ob'];
    	$res=mysql_query("SELECT id
					 FROM 	warehouse_detail
					 WHERE  warehouse_detail.barcode='$ob'");
    	echo mysql_num_rows($res);
    	return 0;

    break;

    case 'save_barcode':
		$barcode_id = $_REQUEST['id'];

		$type 			= $_REQUEST['t'];

    	$client 		= $_REQUEST['bcl'];
		$client_id		= GetClientID($client);

		$cartridge	 	= $_REQUEST['bct'];
		$old_barcode	= $_REQUEST['ob'];
		$count 			= $_REQUEST['c'];
		$comment		= $_REQUEST['com'];

		$cartridge_id	= GetCartridgeID($cartridge);

		$c_date		= date('Y-m-d H:i:s');
		$array		= array();

		if(empty($barcode_id)){
			for($i = 0; $i < $count; $i++){
				$local_id = GetLocalID();
				$barcode = GenerateBarCode($type, $client_id, $cartridge_id, $local_id);
				$array[$i] = AddBarcode($local_id , $user_id, $barcode, $old_barcode, $c_date, $comment);
			}
		}else{
		//	Disable($barcode_id);
		//	$local_id = GetLocalID();
		//	$barcode = GenerateBarCode($type, $client_id, $cartridge_id, $local_id);
		//	$array[0] = SaveBarcode($local_id, $user_id, $barcode_id, $barcode, $old_barcode, $c_date, $comment);
		    edit_barcode($barcode_id) ; 
		}

		$data = array('barcode_list' => json_encode($array));

        break;
    case 'disable':
		$barcode_id = $_REQUEST['id'];
		Disable($barcode_id);

        break;
    default:
       $error = 'Action is Null';
}

$data['error'] = $error;

echo json_encode($data);


/* ******************************
 *	Workers Functions
 * ******************************
 */

function AddBarcode($local_id, $user_id, $barcode, $old_barcode, $c_date, $comment)
{

	if (strlen($old_barcode)==15) {
		//echo $barcode; return 0;
		mysql_query("UPDATE 	warehouse_detail
					 set 	    barcode 	=	'$barcode'
					 WHERE  	barcode 	=	'$old_barcode'");

	}
	mysql_query("INSERT INTO `barcodes`
					(`id`, `user_id`, `code`, `old_barcode`, `create_date`, `comment`)
				 VALUES
					($local_id, $user_id, '$barcode', '$old_barcode', '$c_date', '$comment')");

	return mysql_insert_id();
}

function SaveBarcode($local_id, $user_id, $barcode_id, $barcode, $old_barcode, $c_date, $comment)
{
	mysql_query("UPDATE 	warehouse_detail
					set 	barcode 	=	'$barcode'
				WHERE  	barcode 	=	'$old_barcode' AND warehouse_detail.barcode != NULL");
	mysql_query("INSERT INTO `barcodes`
					(`id`, `user_id`, `code`,`parent`, `old_barcode`, `create_date`, `comment`)
				 VALUES
					($local_id, $user_id, '$barcode',$barcode_id, '$old_barcode', '$c_date', '$comment')");

	return mysql_insert_id();
}
function edit_barcode($barcode_id){
    mysql_query("UPDATE `barcodes`
                 SET    `comment`   = '$_REQUEST[com]'
                 WHERE  `id`        = $barcode_id");
}
function Disable($barcode_id)
{
    mysql_query("UPDATE `barcodes`
				 SET    `actived` = 0
				 WHERE  `id` = $barcode_id");
}

function GetClientID($client_name)
{
	$client_name = htmlspecialchars($client_name, ENT_QUOTES);
	$res = mysql_fetch_assoc(mysql_query("	SELECT	`id`
											FROM	`client`
											WHERE	`name` = '$client_name'"));
	return $res['id'];
}

function GetCartridgeID($cartridge_name)
{
	$cartridge_name = htmlspecialchars($cartridge_name, ENT_QUOTES);
	$res = mysql_fetch_assoc(mysql_query("	SELECT	`id`
											FROM	`production`
											WHERE	`name` = '$cartridge_name'"));
	return $res['id'];
}

function GetLocalID()
{
	GLOBAL $db;
	$id = $db->increment('barcodes');
	return $id;
}

function GetCartridgeType($point)
{
	$data = '';
    $req = mysql_query("SELECT 	`id`,
    						   	`name`
						FROM 	`cartridge_type`");

	if($point == ''){
		$data = '<option value="0" selected="selected"></option>';
	}

	while( $res = mysql_fetch_assoc( $req )){
		if($res['id'] == $point){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}

function GenerateBarCode($type, $client_id, $cartridge_id, $barcode_id){
	switch( strlen($client_id) ){
		case 1 :  $client_id = '000' . $client_id;
			break;
		case 2 :  $client_id = '00' . $client_id;
			break;
		case 3 :  $client_id = '0' . $client_id;
			break;
		case 4 :  $client_id = '' . $client_id;
			break;
	}
	switch( strlen($cartridge_id) ){
		case 1 :  $cartridge_id = '000' . $cartridge_id;
			break;
		case 2 :  $cartridge_id = '00' . $cartridge_id;
			break;
		case 3 :  $cartridge_id = '0' . $cartridge_id;
			break;
	}
	switch( strlen($barcode_id) ){
		case 1 :  $barcode_id = '00000' . $barcode_id;
			break;
		case 2 :  $barcode_id = '0000' . $barcode_id;
			break;
		case 3 :  $barcode_id = '000' . $barcode_id;
			break;
		case 4 :  $barcode_id = '00' . $barcode_id;
			break;
		case 5 :  $barcode_id = '0' . $barcode_id;
			break;
	}
	return $type . $client_id . $cartridge_id . $barcode_id;
}

function GetBarcode($barcode_id)
{
    $res = mysql_fetch_assoc(mysql_query("	SELECT	`barcodes`.`id`,
													SUBSTR(`barcodes`.`code`, 1, 1) AS `type`,
											        `client`.`name` AS `client`,
											        `production`.`name` AS `cartridge`,
    												`barcodes`.`old_barcode` as `old_barcode`,
    												`barcodes`.`comment` as `comment`
											FROM   `barcodes`
											LEFT JOIN `client` ON `client`.`id` = SUBSTR(`barcodes`.`code`, 2, 4)
											LEFT JOIN production ON production.id = SUBSTR(`barcodes`.`code`, 6, 4)
											WHERE   `barcodes`.`id` = $barcode_id"));
	return $res;
}

function GetPrintPage(){
	$data = '
		<div id="dialog-form">
			<fieldset>
				<legend>ძირითადი ინფორმაცია</legend>

		    	<table class="dialog-form-table">
					<tr>
					<td style="width: 170px;">
						<label for="type">შტრიხკოდი</label>
					</td>
					<td>
						<input type="text" id="print_barodeee" class="idle seoy-large" onblur="this.className=\'idle seoy-large\'" onfocus="this.className=\'activeField seoy-large\'" />
					</td>
				</tr>
				</table>
				<!-- ID -->
	        </fieldset>
    	</div>
    ';
	return $data;
}

function GetPage($res = '')
{
	$data = '
	<div id="dialog-form">
	    <fieldset>
	    	<legend>ძირითადი ინფორმაცია</legend>

	    	<table class="dialog-form-table">
				<tr>
					<td style="width: 170px;"><label for="type">ტიპი</label></td>
					<td>
						<select id="type" class="idls">' . GetCartridgeType($res[type]) . '</select>
					</td>
				</tr>
				<tr>
					<td style="width: 170px;"><label for="barcode_client">კლიენტი</label></td>
					<td>
						<div class="seoy-row" id="barcode_client_seoy">
							<input type="text" id="barcode_client" class="idle seoy-large" onblur="this.className=\'idle seoy-large\'" onfocus="this.className=\'activeField seoy-large\'" value="' . $res['client'] . '" />
							<button id="client_btn" class="combobox">barcode_client</button>
						</div>
					</td>
				</tr>
				<tr>
					<td style="width: 170px;"><label for="barcode_cartridge">კარტრიჯი</label></td>
					<td>
						<div class="seoy-row" id="barcode_cartridge_seoy">
							<input type="text" id="barcode_cartridge" class="idle seoy-large" onblur="this.className=\'idle seoy-large\'" onfocus="this.className=\'activeField seoy-large\'" value="' . $res['cartridge'] . '" />
							<button id="barcode_cartridge_btn" class="combobox">barcode_cartridge</button>
						</div>
					</td>
				</tr>
				<tr>
					<td style="width: 170px;"><label for="old_barcode">ძველი შტრიხკოდი</label></td>
					<td>
						<input type="text" id="old_barcode" class="idle seoy-large" onblur="this.className=\'idle seoy-large\'" onfocus="this.className=\'activeField seoy-large\'" value="' . $res['old_barcode'] . '" />
					</td>
				</tr>';
				if(empty($res['id'])){
					$data .= '
					<tr>
						<td style="width: 170px;"><label for="count">რაოდენობა</label></td>
						<td>
							<form>
								<input type="button" value="-" class="btn btn-2 btn-2h1" style="margin-left:0px !important; display: inline; width: 20px;" id="decrement"  onclick="if(this.form.number.value!=1) {this.form.number.value--;}">   <!-- -->
								<input type="text" id="count" name="number" class="idle num" style=" display: inline;" onblur="this.className=\'idle num\'" onfocus="this.className=\'activeField num\'" value="1" min="1" max="2" disabled/>
								<input type="button" value="+" class="btn btn-2 btn-2h2" style="margin-left:0px !important; display: inline; width: 20px;" id="increment" onclick="if(this.form.number.value<10) { this.form.number.value++;} ">  <!--  -->
							</form>

						</td>
					</tr>';
				}
			$data .='
				<tr>
					<td style="width: 170px;"><label for="comment">შენიშვნა</label></td>
					<td valign="top">
						<textarea id="comment" class="idle large" cols="40" rows="3">' . $res['comment'] . '</textarea>
					</td>
				</tr>
			</table>
			<!-- ID -->
			<input type="hidden" id="barcode_id" value="' . $res['id'] . '" />
        </fieldset>
    </div>
    ';
	return $data;
}

?>